﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Reflection;
using AMStudio.Script;
using System.Data;

namespace AMStudio.Common
{
    public static class WorkorderDB
    {
        private static WorkorderDAL db = new WorkorderDAL();
        public static Dictionary<string, WorkOrderItem> woTags;
        public static void Init()
        {
            try
            {
                woTags = db.SelectAllMapper();
            }
            catch (Exception ex)
            {
                throw new Exception("WorkorderDB Init Error:" + ex.Message);
            }
        }

        public static void Add(WorkOrderItem user)
        {
            if (!woTags.ContainsKey(user.Name))
            {
                db.Add(user);
                woTags.Add(user.Name, user);
            }
        }

        public static void Update(WorkOrderItem user)
        {
            //if (woTags.ContainsKey(user.woName))
            //{
            db.Update(user);
            //}
        }

        public static void Delete(string userName)
        {
            if (woTags.ContainsKey(userName))
            {
                db.Delete(userName);
                woTags.Remove(userName);
            }
        }

        private static WorkOrderItem activeOrder = null;
        public static WorkOrderItem ActiveOrder
        {
            get { return WorkorderDB.activeOrder; }
        }
        private static ProductSettings activeProduct = null;
        public static ProductSettings ActiveProduct
        {
            get { return WorkorderDB.activeProduct; }
        }

        public static void GetActiveWorkOrder()
        {
            activeProduct = null;
            activeOrder = null;
            ProductDB db = new ProductDB();
            string foundedProduct = string.Empty;
            foreach (string k in woTags.Keys)
            {
                if (woTags[k].Status == 0)
                {
                    activeOrder = woTags[k];
                    foundedProduct = woTags[k].Product;
                    break;
                }
            }
            activeProduct=db.GetProducts(foundedProduct);
        }

        public static bool ExistWorkorderForProduct(string product)
        {
            foreach (string k in woTags.Keys)
            {
                if (woTags[k].Product == product)
                {
                    return true;
                }
            } 
            return false;
        }

        //public static WorkOrderItem GetActiveOrder()
        //{
        //    ProductDB db = new ProductDB();
        //    string foundedProduct = string.Empty;
        //    WorkOrderItem activeOrder = null;
        //    foreach (string k in woTags.Keys)
        //    {
        //        if (woTags[k].Status == 0)
        //        {
        //            activeOrder = woTags[k];
        //            foundedProduct = woTags[k].Product;
        //            break;
        //        }
        //    }
        //    return activeOrder;
        //}

        //public static ProductSettings GetActiveProduct()
        //{
        //    ProductDB db = new ProductDB();
        //    string foundedProduct = string.Empty;
        //    foreach (string k in woTags.Keys)
        //    {
        //        if (woTags[k].Status == 0)
        //        {
        //            foundedProduct = woTags[k].Product;
        //            break;
        //        }
        //    }
        //    return db.GetProducts(foundedProduct);
        //    //return null;
        //}

        //public static WorkOrderItem CurrentLoginwo = new WorkOrderItem();
    }
}
